﻿#include <QFile>
#include "CRobDataTabWidget.h"
#include "CLogOper.h"
#include "CDevDataBase.h"
#include <QMetaType>

CRobDataTabWidget::CRobDataTabWidget(QWidget *parent)
	: UBasePanel(parent)
{
	ui.setupUi(this);
	m_pInspectionListWidget = nullptr;
	m_pRealTimeDataWidget = nullptr;
	m_pTaskListWidget = nullptr;
	m_pLogWidget = nullptr;
	m_pTimer = new QTimer(this);
	m_pTimer->setTimerType(Qt::TimerType::PreciseTimer);

}

CRobDataTabWidget::~CRobDataTabWidget()
{
	if (m_pInspectionListWidget != NULL)
	{
		delete m_pInspectionListWidget;
		m_pInspectionListWidget = NULL;
	}
	if (m_pRealTimeDataWidget != NULL)
	{
		delete m_pRealTimeDataWidget;
		m_pRealTimeDataWidget = NULL;
	}
	if (m_pTaskListWidget != NULL)
	{
		delete m_pTaskListWidget;
		m_pTaskListWidget = NULL;
	}
	if (m_pInspectionListWidget != NULL)
	{
		delete m_pInspectionListWidget;
		m_pInspectionListWidget = NULL;
	}
	if (m_pLogWidget != NULL)
	{
		CLogOper::instance().destroyLogWidget(m_pLogWidget);
		m_pLogWidget = NULL;
	}
	
}

void CRobDataTabWidget::init()
{
	initConnect();
	//setStyleSheetFile();
	initData();
}

void CRobDataTabWidget::setStyleSheetFile()
{
	/*QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());*/
}

void CRobDataTabWidget::initConnect()
{
	qRegisterMetaType<SDevExecLog>("SDevExecLog");
	connect(m_pDev->m_pDevDataBase, SIGNAL(sigAddDevExecLog(SDevExecLog)), this, SLOT(slotAddExecLog(SDevExecLog)), Qt::QueuedConnection);
	CDevDataBase* pDevDataRob = (CDevDataBase*)m_pDev->m_pDevDataBase;
	connect(m_pTimer, &QTimer::timeout, this, &CRobDataTabWidget::slotDataRefresh);
}

void CRobDataTabWidget::initData()
{
	m_pInspectionListWidget = new CInspectionListWidget(m_sGuid,m_pBasicData,m_pDev,this);
	m_pRealTimeDataWidget = new CRealTimeDataWidget(m_pDev);
	m_pTaskListWidget = new CTaskListWidget(m_sGuid, m_pBasicData, this);
	ui.gridLayoutInspectionList->addWidget(m_pInspectionListWidget);
	ui.gridLayoutRealTimeData->addWidget(m_pRealTimeDataWidget);
	ui.gridLayoutTaskList->addWidget(m_pTaskListWidget);
	m_pLogWidget = CLogOper::instance().createLogWidget(m_pDev->m_pDevDataBase->m_sRobName);
	ui.gridLayoutLog->addWidget(m_pLogWidget);
}

void CRobDataTabWidget::slotAddExecLog(SDevExecLog sSDevExecLog)
{
	SExecLog sLog;
	sLog.m_nAlarmLevel = sSDevExecLog.m_nAlarmLevel;
	sLog.m_sDescriptionl = sSDevExecLog.m_sDescriptionl;
	sLog.m_sDevName = sSDevExecLog.m_sDevName;
	sLog.m_sFuncName = A2T("任务");
	CLogOper::instance().addMsg( sLog);
}


void CRobDataTabWidget::slotDataRefresh()
{
	m_pInspectionListWidget->m_pTimer->start(1000);
	m_pRealTimeDataWidget->m_pTimer->start(1000);
	m_pTaskListWidget->m_pTimer->start(1000);
}